<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts'
//require('echarts/theme/macarons'); // echarts theme
import resize from '../components/mixins/resize'

const animationDuration = 1000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '314px'
    }
  },
  data() {
    return {
      chart: null,
      intervalId: null,
      option: {
        tooltip: {
          trigger: 'axis',
          formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}',
          axisPointer: {
            type: 'shadow',
            label: {
              backgroundColor: '#6a7985'
            }
          },
          textStyle: {
            color: '#fff',
            fontStyle: 'normal',
            fontFamily: '微软雅黑',
            fontSize: 12
          }
        },
        grid: {
          top: 35,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        legend: {
          data: ['走行速度', '走行时间', '在非停车指令情况下机车速度为0的时间'],
          textStyle: {
            color: '#fff'
          }
        },
        xAxis: {
          type: 'category',
          data: [],
          splitLine: {
            show: false
          }
        },
        yAxis: [
          {
            type: 'value',
            boundaryGap: [0, '100%'],
            axisTick: {
              show: false
            },
            splitLine: {
              show: true,
              lineStyle: {
                color: ['#fff'],
                opacity: 0.1
              }
            }
          }
        ],
        // 走行速度、走行时间，在非停车指令情况下机车速度为0的时间
        series: [
          {
            name: '走行速度',
            type: 'line',
            showSymbol: false,
            data: [],
          },
          {
            name: '走行时间',
            type: 'line',
            showSymbol: false,
            data: [],
          },
          {
            name: '在非停车指令情况下机车速度为0的时间',
            type: 'line',
            showSymbol: false,
            data: [],
          }
        ]
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  created() {
    // 设置定时器每秒更新时间
    this.intervalId = setInterval(() => {
      if (this.chart) {
        this.updateData()
      }
    }, 1000)
  },
  beforeDestroy() {
    // 清除定时器，防止内存泄漏
    clearInterval(this.intervalId)

    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    padWithZeros(number) {
      return String(number).padStart(2, '0');
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption(this.option)
    },
    updateData() {
      const val1 = Math.floor(Math.random() * 100 % 99)
      const val2 = Math.floor(Math.random() * 100 % 99)
      const val3 = Math.floor(Math.random() * 100 % 99)

      const now = new Date()
      const axisData = this.padWithZeros(now.getHours()) + ':' + this.padWithZeros(now.getMinutes()) + ':' + this.padWithZeros(now.getSeconds())
      this.option.series[0].data.push(val1)
      this.option.series[1].data.push(val2)
      this.option.series[2].data.push(val3)
      this.option.xAxis.data.push(axisData)
      this.chart.setOption(this.option)
 
      // 清除旧数据，保持数据长度不变
      if (this.option.series[0].data.length > 10) {
        for (let i = 0; i < 3; i++) {
          this.option.series[i].data.shift()
        }
        this.option.xAxis.data.shift()
      }

      // console.log('updateData...', axisData)
      // console.log('this.option.xAxis.data:', this.option.xAxis.data)
      // console.log('this.option.series[0]:', this.option.series[0])
    }
  }
}
</script>
